import Layout from '@/layOut/index'
import router from '@/router'
import { getRouters } from '@/apis/menuManage'

// import { menuData } from "@/utils/data"
// import dynamicRoutes from "@/router/dynamicRoutes"
import constantRoutes from "@/router/constantRoutes"

const state = {
    // 菜单列表
    menuList: []
}

const mutations = {
    // 设置菜单
    setMenu(state, data) {
        state.menuList = data
    },

    // 选择菜单，标记出来
    selectMenu(state, path) {
        let menuList = state.menuList

        menuList.forEach(item => {
            item.active = false

            if (item.meta.fatherPath === path) {
                item.active = true
            }
        })

        state.menuList = menuList
    }
}

const actions = {
    // 获取菜单列表
    async getMenuList(ctx) {
        let data = await getRouters()
        let dynamicMenu = []

        if (data.code === 200) {
            dynamicMenu = data.data
        }

        let constantMenu = JSON.parse(JSON.stringify(constantRoutes))

        ctx.commit("setMenu", constantMenu.concat(dynamicMenu))
    },
}

// 遍历路由
function mapMenu(data) {
    data.forEach((item) => {
        if (item.component === "LayOut") {
            item.component = Layout
        }
    })

    return data
}

const getters = {}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}
